<template>
    <view :class="topFlag === true ? 'gotop' : 'gotop hide'" @tap="toTop">
        <uni-icons type="top" size="26" color="#666"></uni-icons>
        <view class="word">TOP</view>
    </view>
</template>

<script>
export default {
    name:"ToTop",
    data: function() {
        return {
            scrollTop: 0,
            topFlag:false
        }
    },
    mounted() {
        var that = this;
        uni.$on('onPageScroll', function(data) {
            that.topFlag = data > 800
        });
    },
    methods: {
        // 返回顶部
        toTop: function() {
            uni.pageScrollTo({
                scrollTop: 0,
                duration: 500   //动画效果
            })
        }
    }
}
</script>

<style lang="scss">
.gotop {
    width: 88rpx;
    height: 88rpx;
    position: fixed;
    bottom: calc(230rpx + env(safe-area-inset-bottom));
    right: 20rpx;
    border-radius: 50%;
    z-index: 995;
    background: rgba(255, 255, 255, .85);
    box-shadow: 0 2px 8px 0 rgba(0, 0, 0, .16);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    box-sizing: border-box;
    transition: transform .3s;
    flex-direction: column;
    .iconfont {
        font-size: 42rpx;
    }
}
.gotop .word{ margin-top:-5px; color:#666; font-size: 12px;}
.hide {
    transform: translate3d(100px, 0, 0);
}
</style>
